import type { ValidationRule } from '../types/dynamicForm';
export const required = (message: string = '此字段为必填项'): ValidationRule => ({
  validator: (value: any) => {
    if (typeof value === 'string') return value.trim() !== '';
    if (Array.isArray(value)) return value.length > 0;
    return value !== undefined && value !== null;
  },
  message
});

export const minLength = (min: number, message: string): ValidationRule => ({
  validator: (value: string) => value.length >= min,
  message
});

export const maxLength = (max: number, message: string): ValidationRule => ({
  validator: (value: string) => value.length <= max,
  message
});

export const email = (message: string = '请输入有效的邮箱地址'): ValidationRule => ({
  validator: (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
  message
});

export const pattern = (regex: RegExp, message: string): ValidationRule => ({
  validator: (value: string) => regex.test(value),
  message
});